package com.iplant.ams.service.po.dms.scada.status;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.ams.service.basicenum.dms.scada.DMSDeviceStatusEnum;
import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceAlarm;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.condition.DBCondition;
import com.iplant.base.utils.annotation.field.DBField;
import com.iplant.base.utils.annotation.join.DBJoin;
import com.iplant.base.utils.annotation.join.multi.DBJoinMulti;
import com.iplant.base.utils.annotation.po.DBBasePO;

import java.io.Serializable;
import java.util.Calendar;

@DBBasePO(TableName = "dms_device_hisstatus", Schemas = DBSchemas.AMS, CheckName = {"DeviceID,StartDate"},
        Code = false, Name = false, Comment = "设备历史状态")
public class DMSDeviceHisStatus extends BasePO implements Serializable {
    private static final long serialVersionUID = 1L;

    @DBCondition
    @DBField(name="DeviceID",Description = "设备AssetNo")
    public String AssetNo = "";


    @DBCondition
    @DBField(name = "DeviceStatus", Description = "设备当前状态")
    public int Status = 0;

    @DBCondition
    @DBField(Description = "设备显示状态")
    public int DeviceActStatus = 0;

    @DBCondition(CompareType = DBCondition.DBCompareType.geLe, ParamName = "UDStartTime", ParamName2 = "EndTime")
    @DBField
    public Calendar StartDate = Calendar.getInstance();
    @DBField
    public Calendar EndDate = Calendar.getInstance();

    @DBField(Description = "持续时长(s)")
    public int Duration = 0;

    @DBCondition(CompareType = DBCondition.DBCompareType.geLe, ParamName = "StartTime", ParamName2 = "UDEndTime")
    @DBField
    public Calendar UpdateDate = Calendar.getInstance();


    public DMSDeviceStatus CreateDeviceStatus(DMSDeviceLedger wDMSDeviceLedger) {
        DMSDeviceStatus wDeviceStatus = new DMSDeviceStatus();

        wDeviceStatus.ID = this.ID;
        wDeviceStatus.AssetNo = this.AssetNo;
        wDeviceStatus.Status = this.Status;
        wDeviceStatus.Duration = this.Duration;
        wDeviceStatus.StatusTime = this.StartDate;
        wDeviceStatus.StatusTimeEnd = this.UpdateDate;
        wDeviceStatus.ActStatus=this.getDeviceActStatus();
        if (wDMSDeviceLedger != null) {
            wDeviceStatus.DeviceNo = wDMSDeviceLedger.Code;
            wDeviceStatus.DeviceName = wDMSDeviceLedger.Name;
            wDeviceStatus.DeviceID = wDMSDeviceLedger.ID;
            wDeviceStatus.ModelID = wDMSDeviceLedger.ModelID;
            wDeviceStatus.ModelName = wDMSDeviceLedger.StandardModelName;
            wDeviceStatus.ModelNo = wDMSDeviceLedger.StandardModelCode;
            wDeviceStatus.DeviceType = wDMSDeviceLedger.DeviceTypeID;
            wDeviceStatus.DeviceTypeName = wDMSDeviceLedger.DeviceTypeName;
            wDeviceStatus.DeviceTypeCode = wDMSDeviceLedger.DeviceTypeCode;
            wDeviceStatus.WorkShopID = wDMSDeviceLedger.WorkshopID;
            wDeviceStatus.WorkShopName = wDMSDeviceLedger.WorkshopName;
            wDeviceStatus.WorkShopCode = wDMSDeviceLedger.WorkshopCode;
            wDeviceStatus.FactoryID = wDMSDeviceLedger.FactoryID;
            wDeviceStatus.FactoryName = wDMSDeviceLedger.FactoryName;
            wDeviceStatus.FactoryCode = wDMSDeviceLedger.FactoryCode;
            wDeviceStatus.AreaID = wDMSDeviceLedger.WorkAreaID;
            wDeviceStatus.AreaNo = wDMSDeviceLedger.WorkAreaCode;
            wDeviceStatus.PositionText = wDMSDeviceLedger.WorkAreaName;
            wDeviceStatus.ImageIcon = wDMSDeviceLedger.ImageIcon;
            wDeviceStatus.Remark = wDMSDeviceLedger.Description;
        }

        return wDeviceStatus;
    }



    @JsonProperty("AssetNo")
    public String getAssetNo() {
        return AssetNo;
    }

    public void setAssetNo(String assetNo) {
        AssetNo = assetNo;
    }

    @JsonProperty("Status")
    public int getStatus() {
        return Status;
    }

    public void setStatus(int status) {
        Status = status;
        getDeviceActStatus();
    }

    @JsonProperty("UpdateDate")
    public Calendar getUpdateDate() {
        return UpdateDate;
    }

    public void setUpdateDate(Calendar updateDate) {
        UpdateDate = updateDate;
    }

    @JsonProperty("DeviceActStatus")
    public int getDeviceActStatus() {

        if ((Status & DMSDeviceStatusEnum.TurnOn.getValue()) == 0) {
            DeviceActStatus = 0;
        } else if ((Status & (DMSDeviceStatusEnum.Run.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.Run.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.EmergencyStop.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.EmergencyStop.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.Alarm.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.Alarm.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.Stop.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.Stop.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.Manual.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.Manual.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.Auto.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.Auto.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.Wait.getValue())) > 0) {
            DeviceActStatus = DMSDeviceStatusEnum.Wait.getValue();
        } else if ((Status & (DMSDeviceStatusEnum.TurnOn.getValue())) > 0) {
            DeviceActStatus = (DMSDeviceStatusEnum.TurnOn.getValue());
        } else {
            DeviceActStatus = 0;
        }

        return DeviceActStatus;
    }

    public void setDeviceActStatus(int deviceActStatus) {
        DeviceActStatus = deviceActStatus;
    }

    @JsonProperty("StartDate")
    public Calendar getStartDate() {
        return StartDate;
    }

    public void setStartDate(Calendar startDate) {
        StartDate = startDate;
    }

    @JsonProperty("EndDate")
    public Calendar getEndDate() {
        return EndDate;
    }

    public void setEndDate(Calendar endDate) {
        EndDate = endDate;
    }

    @JsonProperty("Duration")
    public int getDuration() {
        return Duration;
    }

    public void setDuration(int duration) {
        Duration = duration;
    }
}
